# OTA项目Makefile

# 变量定义
BACKEND_DIR = ota-backend
FRONTEND_DIR = ota-frontend
WEB_DIR = $(BACKEND_DIR)/web
GO = go
NODE = node
NPM = npm

# 默认目标
.PHONY: all
all: build

# 构建后端
.PHONY: build-backend
build-backend:
	@echo "构建Go后端..."
	cd $(BACKEND_DIR) && $(GO) build -o ota-server .

# 运行后端
.PHONY: run-backend
run-backend:
	@echo "启动Go后端服务..."
	cd $(BACKEND_DIR) && $(GO) run .

# 构建前端
.PHONY: build-frontend
build-frontend:
	@echo "构建Vue3前端..."
	cd $(FRONTEND_DIR) && $(NPM) run build

# 构建前端到后端web目录
.PHONY: build-frontend-to-backend
build-frontend-to-backend:
	@echo "构建前端到后端web目录..."
	cd $(FRONTEND_DIR) && $(NPM) run build
	@echo "创建web目录..."
	mkdir -p $(WEB_DIR)
	@echo "复制前端构建文件到后端web目录..."
	rm -rf $(WEB_DIR)/*
	cp -r $(FRONTEND_DIR)/dist/* $(WEB_DIR)/
	@echo "前端文件已复制到 $(WEB_DIR)"

# 运行前端开发服务器
.PHONY: run-frontend
run-frontend:
	@echo "启动Vue3前端开发服务器..."
	cd $(FRONTEND_DIR) && $(NPM) run dev

# 安装前端依赖
.PHONY: install-frontend
install-frontend:
	@echo "安装前端依赖..."
	cd $(FRONTEND_DIR) && $(NPM) install

# 安装后端依赖
.PHONY: install-backend
install-backend:
	@echo "安装后端依赖..."
	cd $(BACKEND_DIR) && $(GO) mod tidy

# 构建所有
.PHONY: build
build: build-backend build-frontend

# 构建生产版本（前端集成到后端）
.PHONY: build-prod
build-prod: build-frontend-to-backend build-backend
	@echo "生产版本构建完成！"

# 安装所有依赖
.PHONY: install
install: install-backend install-frontend

# 开发模式（同时运行前后端）
.PHONY: dev
dev:
	@echo "启动开发模式..."
	@make -j2 run-backend run-frontend

# 清理构建文件
.PHONY: clean
clean:
	@echo "清理构建文件..."
	cd $(BACKEND_DIR) && rm -f ota-server
	cd $(FRONTEND_DIR) && rm -rf dist
	rm -rf $(WEB_DIR)

# 测试后端
.PHONY: test-backend
test-backend:
	@echo "测试Go后端..."
	cd $(BACKEND_DIR) && $(GO) test ./...

# 测试前端
.PHONY: test-frontend
test-frontend:
	@echo "测试Vue3前端..."
	cd $(FRONTEND_DIR) && $(NPM) run test

# 测试所有
.PHONY: test
test: test-backend test-frontend

# 格式化代码
.PHONY: format
format:
	@echo "格式化Go代码..."
	cd $(BACKEND_DIR) && $(GO) fmt ./...
	@echo "格式化前端代码..."
	cd $(FRONTEND_DIR) && $(NPM) run format

# 帮助信息
.PHONY: help
help:
	@echo "OTA项目Makefile命令:"
	@echo "  install                    - 安装所有依赖"
	@echo "  install-backend            - 安装后端依赖"
	@echo "  install-frontend           - 安装前端依赖"
	@echo "  build                      - 构建所有项目"
	@echo "  build-backend              - 构建Go后端"
	@echo "  build-frontend             - 构建Vue3前端"
	@echo "  build-frontend-to-backend  - 构建前端到后端web目录"
	@echo "  build-prod                 - 构建生产版本（前端集成到后端）"
	@echo "  run-backend                - 运行Go后端"
	@echo "  run-frontend               - 运行Vue3前端开发服务器"
	@echo "  dev                        - 开发模式（同时运行前后端）"
	@echo "  test                       - 运行所有测试"
	@echo "  test-backend               - 测试Go后端"
	@echo "  test-frontend              - 测试Vue3前端"
	@echo "  format                     - 格式化代码"
	@echo "  clean                      - 清理构建文件"
	@echo "  help                       - 显示帮助信息" 